package com.su.lightthings.utils

import android.content.Intent
import android.provider.MediaStore
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.jeremyliao.liveeventbus.LiveEventBus
import com.su.lightthings.bean.PermissionResultBean
import com.su.lightthings.viewmodel.CommonViewModel
import java.lang.ref.WeakReference

object CameraUtil {
    private val CAMERA_CODE: Int = 1
    private var weakReference: WeakReference<AppCompatActivity>? = null
    private const val TAG = "CameraUtil"
    private var tempEvent = -1

    fun setActivity(activity: AppCompatActivity) {
        weakReference = WeakReference(activity)
        observer()
    }

    private fun getActivity(): AppCompatActivity? {
        return weakReference?.get()
    }

    fun control(type: Int) {
        if (getActivity()?.let { XXPermissions.isGranted(it, Permission.CAMERA) } == true) {
            when (type) {
                1 -> openCamera()
                2 -> closeCamera()
                else -> {}
            }
        } else {
            tempEvent = type
            getActivity()?.let {
                CommonViewModel.INSTANCE.doPermissionTask(
                    Cons.LiveDataBus.PERMISSION_CAMERA_INDEX,
                    it
                )
            }
        }

    }

    private fun observer() {
        getActivity()?.let { it ->
            LiveEventBus
                //权限申请结果
                .get(Cons.LiveDataBus.GET_PERMISSION_RESULT, PermissionResultBean::class.java)
                .observe(it) {
                    Log.d(TAG, "权限申请事件: $it")
                    when (it.id) {
                        //消息通知权限申请
                        Cons.LiveDataBus.PERMISSION_CAMERA_INDEX -> {
                            if (it.result == Cons.LiveDataBus.GET_PERMISSION_OK) {
                                when (tempEvent) {
                                    1 -> openCamera()
                                    2 -> closeCamera()
                                    else -> {}
                                }
                            }
                            if (it.result == Cons.LiveDataBus.GET_PERMISSION_FAILED) {
                            }
                            tempEvent = -1
                        }
                    }
                }
        }
    }

    /**
     * 打开相机
     */
    private fun openCamera() {
        val activity = weakReference?.get()
        val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (activity != null) {
            if (cameraIntent.resolveActivity(activity.packageManager) != null) {
                activity.startActivityForResult(cameraIntent, CAMERA_CODE)
            } else {
                // Handle the case where there is no camera app available
                // You can display a message to the user or take some other action
            }
        }
    }

    /**
     * 关闭相机
     */
    private fun closeCamera() {
        getActivity()?.finishActivity(CAMERA_CODE)
    }
}